/*
 * @LastEditTime: 2023-03-30 23:11:33
 * @Method: 
 * @Description: 
 */
package CodeTop;

public class Solution82 {
    // public ListNode deleteDuplicates(ListNode head) {
    //     ListNode dummy = new ListNode();
    //     dummy.next = head;
    //     ListNode pre = dummy, cur = head;
    //     while (cur != null) {
    //         while (cur.next != null && cur.val == cur.next.val) {
    //             cur = cur.next;
    //         }
    //         if (pre.next == cur) {
    //             pre = cur;
    //             cur = pre.next;
    //         } else {
    //             pre.next = cur.next;
    //             cur = pre.next;
    //         }
    //     }
    //     return dummy.next;
    // }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode cur = head;
        while (cur.next != null && cur.val == cur.next.val) {
            cur = cur.next;
        }
        if (head == cur) {
            head.next = deleteDuplicates(head.next);
            return head;
        } else {
            return deleteDuplicates(cur.next);
        }
    }
}
